layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_col_form';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
			t.c.data = c.data; // 数据
			
			/**自定义的字段，菜单ID**/
			t.c.sysMenuId = c.sysMenuId;
		},
        load:function () { // 加载控件
        	var h = ''
        		+ '<div class="layui-form layui-form-item" lay-filter="' + this.id + '">'
	        		+ '<div class="layui-col-md12">'
		        		+ '<div class="layui-input-block">'
		        			+ '<button type="button" class="layui-btn layui-btn-primary btn-add-sys_col_all"><i class="layui-icon layui-icon-addition"></i> 添加字段（总）</button>'
		        			+ '<button type="button" class="layui-btn layui-btn-primary btn-add-sys_col_form"><i class="layui-icon layui-icon-set-sm"></i> 配置字段（表单）</button>'
		        		+ '</div>'
	        		+ '</div>'
        		+ '</div>'
            	+ '<div class="layui-form layui-form-item" pane>'
            		+ '<label class="layui-form-label">字段(表单)</label>'
            		+ '<div class="layui-input-block" id="'+this.c.tableId+'">'
            			+ this.loadTable()
            		+ '</div>'
        		+ '</div>'
    		+ '</div>';
            $('#'+this.c.id).html(h);
            this.listenBtn();
        },
        loadTable:function () { // 加载表格
            return '<table class="layui-table" style="width: 98%;" lay-skin="row" style="margin:0"><thead>'+this.loadTableOfThead()+'</thead><tbody>'+this.loadTableOfTbody()+'</tbody></table>';
        },
        loadTableOfThead:function () { // 加载表头
            var h = '<thead><tr class="layui-bg-blue">';
            h += '<th style="width:8%; text-align: center;">字段编码</th>';
            h += '<th style="width:8%; text-align: center;">字段名称</th>';
            h += '<th style="width:8%; text-align: center;">字段类型</th>';
            h += '<th style="width:8%; text-align: center;">是否新增</th>';
            h += '<th style="width:8%; text-align: center;">是否编辑</th>';
            h+= '<th style="width:15%; text-align: center;">操作</th></tr></thead>';
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = '<tbody>';
            for(var i=0;i<t.c.data.length;i++){
	            h += '<tr>';
	            h += '<td style="text-align:center;">' + t.c.data[i].code + '</td>';
	            h += '<td style="text-align:center;"><input class="layui-input" data-index="' + i + '" data-type="name" lay-filter="_input"  value="' + t.c.data[i].name + '" ></td>';
	            h += '<td style="text-align:center;">' + this.loadSelectorOfType(i, t.c.data[i].type) + '</td>';
	            h += '<td style="text-align:center;"><input type="checkbox" data-index="' + i + '" ' + (t.c.data[i].stateAdd == 1 ? 'checked=""' : '') + ' data-type="stateAdd" lay-skin="switch" lay-filter="_switch" lay-text="是|否"></td>';
	            h += '<td style="text-align:center;"><input type="checkbox" data-index="' + i + '" ' + (t.c.data[i].stateEdit == 1 ? 'checked=""' : '') + ' data-type="stateEdit" lay-skin="switch" lay-filter="_switch" lay-text="是|否"></td>';
                h += '<td style="text-align:center;"><a class="layui-btn layui-btn-danger layui-btn-sm" data-index="' + i + '" lay-filter="remove"><i class="layui-icon layui-icon-delete"></i>移除</a><a class="layui-btn layui-btn-sm" id="id_' + new Date().getTime() + '_' + i + '" data-index="'+i+'" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>配置</a></td>'
                h+= '</tr>';
            }
            
            h += '</tbody>';
			return h;
        },
        loadSelectorOfType:function (index, type) { // 加载下拉框-类型
        	var h = '<select name="type" data-index="' + index + '" data-type="type" lay-filter="_selector">';
        	
            h += '<option value="1"' + (type == 1 ? 'selected' : '') + '>文本框</option>';
            h += '<option value="2"' + (type == 2 ? 'selected' : '') + '>选择框</option>';
            h += '<option value="3"' + (type == 3 ? 'selected' : '') + '>富文本</option>';
            h += '<option value="4"' + (type == 4 ? 'selected' : '') + '>图片</option>';
            h += '<option value="5"' + (type == 5 ? 'selected' : '') + '>日期</option>';
            h += '<option value="11"' + (type == 11 ? 'selected' : '') + '>文件</option>';
            
            h += '</select>';
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
            // 监听“配置字段（表单）”按钮
            $('#' + t.c.id).find('.btn-add-sys_col_form').on('click',function (e) {
            	var sysColIds = '';
	            for(var i=0;i<t.c.data.length;i++){
	            	sysColIds += t.c.data[i].sysColId + ','
	            }
	            if (sysColIds.length > 0) {
	            	sysColIds = sysColIds.substring(0, sysColIds.length - 1);
				}
	            
            	var w=($(window).width()*0.9);
            	var h=($(window).height() - 50);
                layer.open({
                    type: 2,
                    area: [w+'px', h +'px'],
                    fix: true,
                    maxmin: false,
                    shadeClose: true,
                    shade:0.4,
                    title: "选择客盘账号",
                    content: '/web/sys/sysColForm/toSelectSysCol?sysColIds=' + sysColIds + '&sysMenuId=' + t.c.sysMenuId
                });
            });
            // 监听“添加字段（总）”按钮
            $('#' + t.c.id).find('.btn-add-sys_col_all').on('click',function (e) {
	            
            	var w=($(window).width()*0.5);
            	var h=($(window).height()* 0.8);
				layer.open({
					type: 2,
					title: "新增字段（总）",
					area: ['95%', '90%'],
					content: '/web/sys/sysCol/toAdd?sysMenuId=' + t.c.sysMenuId
				});
            });
            // 监听“配置”
            $('#' + t.c.tableId).find('a[lay-filter="edit"]').on('click',function (e) {
                var i = $(this).attr('data-index');
            	var w=($(window).width()*0.9);
            	var h=($(window).height() - 50);
                layer.open({
                    type: 2,
                    area: [w+'px', h +'px'],
                    fix: true,
                    maxmin: false,
                    shadeClose: true,
                    shade:0.4,
                    title: "配置",
                    content: '/web/sys/sysColForm/toSysDictOfSelector?id=' + t.c.data[i]['id']
                });
            });
            
            // 表单控件重新加载
        	form.render();
            
			// 监听“下拉框”
			form.on('select(_selector)', function(obj){
				var index = $(obj.elem).attr('data-index');
				var key = $(obj.elem).attr('data-type');
				var id = t.c.data[index]['id'];
				$.ajax({
					type: "POST",
					url: '/api/inter/sysColForm/edit',
					data: {id: id, key: key, value: obj.value},
					success: function (res) {
						layer.msg(res.msg);
						if (res.code == 0) {
							t.c.data[index][key] = obj.value;
						}
					}
				});
				return;
			});
            
			// 监听“是否新增/是否编辑”
			form.on('switch(_switch)', function(obj){
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).prop("checked") ? 1 : 0;
				var id = t.c.data[index]['id'];
				$.ajax({
					type: "POST",
					url: '/api/inter/sysColForm/edit',
					data: {id: id, key: key, value: value},
					success: function (res) {
						layer.msg(res.msg);
						if (res.code == 0) {
							t.c.data[index][key] = value;
						}
					}
				});
				return;
			});
            
			// 监听“商品编码”输入框
			$('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				var id = t.c.data[index]['id'];
				$.ajax({
					type: "POST",
					url: '/api/inter/sysColForm/edit',
					data: {id: id, key: key, value: value},
					success: function (res) {
						layer.msg(res.msg);
						if (res.code == 0) {
							t.c.data[index][key] = value;
						}
					}
				});
				return;
			});
            
            // 监听“移除”
            $('#' + t.c.tableId).find('a[lay-filter="remove"]').on('click',function (e) {
				var index = $(this).attr('data-index');
				var id = t.c.data[index]['id'];
				$.ajax({
					type: "POST",
					url: '/api/inter/sysColForm/deleteTrue',
					data: {id: id},
					success: function (res) {
						layer.msg(res.msg);
						if (res.code == 0) {
							t.c.data.splice(index, 1);
							t.reload(t.c);
						}
					}
				});
			});
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        }
	};
	exports('simple_table_col_form', object);
});